package com.lanou.moving.utils;

import android.app.Activity;
import android.content.SharedPreferences;
import android.widget.RelativeLayout;

import com.lanou.moving.R;

/**
 * Created by dllo on 15/11/6.
 */
public class SkinSettingManager {

    public final static String SKIN_PREF = "skinSrting";
    public SharedPreferences preferences;
    private int[] skinResources = {R.mipmap.mai_1, R.mipmap.lan,
            R.mipmap.mai_1, R.mipmap.lan,
            R.mipmap.mai_1,
            R.mipmap.lan
    };

    private Activity mActivity;
    private RelativeLayout mLayout;

    public SkinSettingManager(Activity activity, RelativeLayout layout) {
        this.mActivity = activity;
        this.mLayout = layout;

        preferences = mActivity.getSharedPreferences(SKIN_PREF, 3);

    }

    public SkinSettingManager(Activity activity) {
        this.mActivity = activity;
        preferences = mActivity.getSharedPreferences(SKIN_PREF, 3);
    }

    public int getSkinType() {
        String key = "skin_type";
        return preferences.getInt(key, 0);
    }

    public void setSkinType(int j) {
        SharedPreferences.Editor editor = preferences.edit();
        String key = "skin_type";

        editor.putInt(key, j);
        editor.commit();
    }

    public int getCurrentSkinRes() {
        int skinLen = skinResources.length;
        int getSkinLen = getSkinType();

        if (getSkinLen >= skinLen) {
            getSkinLen = 0;
            return skinResources[getSkinLen];
        } else {
            return skinResources[getSkinLen];
        }


    }

    public void toggleSkins(int skinType) {
        setSkinType(skinType);
        mActivity.getWindow().setBackgroundDrawable(null);
        try {
            mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes());
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public void initSkins() {
        if (mLayout == null) {
            mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes());

        } else {
            mLayout.setBackgroundResource(getCurrentSkinRes());
        }
    }

}
